<?php
namespace app\flash\controller;
use QL\QueryList;
use think\Db;
use think\Controller;
use think\Exception;
use think\Common;
class Caiji extends Controller
{
    public function coin(){
      /*$url = 'https://www.bishijie.com/coins/top/500';*/
        $url = 'https://www.bishijie.com/coins/all';
      $rulers = [
        //文章标题
        'coin' => ['.coinname a','text'],
          'jiancheng'=>['#table_body tr','data-slug'],
        'coin_img' => ['.coinname a img','src'],
        'href'=>['.coinname a','href'],
          'price'=>['.coinname + td a','text'],
          'zhangdie'=>['.percent','text'],
          'volume'=>['td.volume','text'],
          'market_cap'=>['.market_cap','text'],
      ];
      $data = QueryList::Query($url,$rulers)->data;

      foreach ($data as $key => $value)
      {
        $coin = isset($value['coin'])?$value['coin']:'-';
        $fenge = explode('-',trim($coin));
          $jiancheng = isset($value['jiancheng'])?$value['jiancheng']:'-';
          $jiancheng = explode('-',trim($jiancheng));
          array_shift($jiancheng);
          $href = isset($value['href'])?$value['href']:'//';
          $mingcheng = explode('/',trim($href));
        $arr[] = array(
         'mingcheng'=>$mingcheng[2],
        'jiancheng'=>$jiancheng[0],
        'abbreviation' => $fenge[0],//大写英语名字
        'currency'=>isset($fenge[1])?$fenge[1]:'',//汉语名字
        'img'  => isset($value['coin_img'])?$value['coin_img']:'',
        );
        $zhangdie = $value['zhangdie'];
          $zhangdie = (float)$zhangdie/100;
          $arr2[] = array(
            'price' => isset($value['price'])?$value['price']:'',
            'zhangdie'=> $zhangdie,
            'volume' =>isset($value['volume'])?$value['volume']:'',
            'market_cap'=>isset($value['market_cap'])?$value['market_cap']:'',
        );
      }
      $count = count($arr);
      for ($i=0; $i < $count; $i++) {
          if(trim($arr[$i]['mingcheng']) ==''){
              break;
          }
          //判断货币表是否存在，不在则插入,存在则更新
          $iscurrency = Db::name('currency')->where('mingcheng',$arr[$i]['mingcheng'])->find();
          if(empty($iscurrency)){
              //插入
              $currency_id = Db::name('currency')->insertGetId($arr[$i]);
          }else{
              //更新
              Db::name('currency')->where('mingcheng',$arr[$i]['mingcheng'])->update($arr[$i]);
              $currency_id = $iscurrency['id'];
          }
          $ishangqing = Db::name('currency_hangqing')->where('currency_id',$currency_id)->find();
          if(empty($ishangqing)){
              $arr2[$i]['currency_id'] = $currency_id;
              //插入
              $hangqing = Db::name('currency_hangqing')->insertGetId($arr2[$i]);
          }else{
              //更新
              $hangqing =  Db::name('currency_hangqing')->where('currency_id',$currency_id)->update($arr2[$i]);
          }
      }
      var_dump('采集成功');exit();
    }

    //虚拟货币表采集
    public function coin2(){
        ini_set("max_execution_time", 0);
        $url = 'https://dncapi.bqiapp.com/api/coin/data_list?webp=1';
        $response = curl_https($url,array(), 5);
        $response = json_decode($response,true);
        if($response['code']==200){
            $arr = $response['data'];
            $count = count($arr);
            for ($i=0; $i < $count; $i++) {
                if($arr[$i]['type']==1){
                    //判断货币表是否存在，不在则插入,存在则更新
                    $iscurrency = Db::name('currency')->where(array('code'=>$arr[$i]['code']))->find();
                    if(empty($iscurrency)){
                        //插入
                        $currency_id = Db::name('currency')->insertGetId($arr[$i]);
                    }else{
                        //更新
                        Db::name('currency')->where(array('code'=>$arr[$i]['code']))->update($arr[$i]);
                        $currency_id = $iscurrency['id'];
                    }
                }

            }
        }
        echo '采集成功';
    }

    //货币汇率采集
    public function coin_rate(){
        ini_set("max_execution_time", 0);
        $url = 'https://dncapi.bqiapp.com/api/coin/web-rate?webp=1';
        $response = curl_https($url,array(), 5);
        $response = json_decode($response,true);
        echo '<pre>';
        var_dump($response);die;
        if($response['code']==200){
            $arr = $response['data'];
            $count = count($arr);
            for ($i=0; $i < $count; $i++) {
                if($arr[$i]['type']==1){
                    //判断货币表是否存在，不在则插入,存在则更新
                    $iscurrency = Db::name('currency')->where(array('code'=>$arr[$i]['code']))->find();
                    if(empty($iscurrency)){
                        //插入
                        $currency_id = Db::name('currency')->insertGetId($arr[$i]);
                    }else{
                        //更新
                        Db::name('currency')->where(array('code'=>$arr[$i]['code']))->update($arr[$i]);
                        $currency_id = $iscurrency['id'];
                    }
                }

            }
        }
        echo '采集成功';
    }

    public function hangqing(){
        ini_set("max_execution_time", 0);
        $j = 0;
        for($i=1;$i<=100;$i++){
            $url = 'https://dncapi.bqiapp.com/api/coin/web-coinrank?page='.$i.'&type=-1&pagesize=100&webp=1';
            $response = curl_https($url,array(), 30);
            $response = json_decode($response,true);
            if(count($response['data'])>0){
                $arr[$i] =array(
                    'current_price'=>$response['data'][$i]['current_price'],
                    'current_price_usd'=>$response['data'][$i]['current_price_usd'],
                    'code'=>$response['data'][$i]['code'],
                    'change_percent'=>$response['data'][$i]['change_percent'],
                    'market_value'=>$response['data'][$i]['market_value'],
                    'vol'=>$response['data'][$i]['vol'],
                    'supply'=>$response['data'][$i]['supply'],
                    'kline_data'=>$response['data'][$i]['kline_data'],
                    'market_value_usd'=>$response['data'][$i]['market_value_usd'],
                    'vol_usd'=>$response['data'][$i]['vol_usd'],
                    'marketcap'=>$response['data'][$i]['marketcap'],
                    'turnoverrate'=>$response['data'][$i]['turnoverrate'],
                );
                //判断货币表是否存在，不在则插入,存在则更新
                $iscurrency = Db::name('currency_hangqing')->where(array('code'=>$arr[$i]['code']))->find();
                if(empty($iscurrency)){
                    //插入
                    $currency_id = Db::name('currency_hangqing')->insertGetId($arr[$i]);
                }else{
                    //更新
                    Db::name('currency_hangqing')->where(array('code'=>$arr[$i]['code']))->update($arr[$i]);
                    $currency_id = $iscurrency['id'];
                }
            }
            $j++;
        }
        echo $j;
    }



}
